/**********************************************
** @Des: This file ...
** @Author: xiaomeng79
** @Date:   17-11-24 下午4:58
***********************************************/
package libs

import (
	"time"
	"github.com/astaxie/beego"
	"strconv"
	"strings"
)

/**
格式化时间
 */
func TimeFormat(t int64) string {
	return time.Unix(t, 0).Format("2006-01-02 15:04:05")
}

/**
uint8转字符串
 */
func IntToString(t uint8) string {
	return strconv.Itoa(int(t))
}


/**
string to int
 */
func StringToInt(s string) int {
	i,_ := strconv.Atoi(s)
	return i
}

/**
字符串转int
 */
func StringToInt64(s string) int64 {
	i, _ := strconv.ParseInt(s, 10, 64)
	return i
}

/**
显示父级别的等级数
 */
func ShowCatLevel(s string) string {
	return strings.Repeat("-",strings.Count(s,"-")-1)
}

/**
显示父级别的等级数
 */
func ShowCatLevelKong(s string) string {
	return strings.Repeat("&ensp;&ensp;&ensp;",strings.Count(s,"-")-1)
}

/**
是否在数组中
 */
func In_array(i int, arr []string) bool {
	//_s,_ := strconv.Atoi(s)
	for _,v := range arr {
		if _v,_ := strconv.Atoi(v); _v == i {
			return true
		}
	}
	return false
}

/**
字符串转数组
 */
func StringToArray(s interface{}, sep string) []string {
	if _s,ok := s.(string);ok {
		return strings.Split(_s,sep)
	}
	return make([]string,0)
}

func Init () {
	beego.AddFuncMap("TimeFormat",TimeFormat)
	beego.AddFuncMap("StringToArray",StringToArray)
	beego.AddFuncMap("In_array",In_array)
	beego.AddFuncMap("IntToString",IntToString)
	beego.AddFuncMap("StringToInt",StringToInt)
	beego.AddFuncMap("StringToInt64",StringToInt64)
	beego.AddFuncMap("ShowCatLevel",ShowCatLevel)
	beego.AddFuncMap("ShowCatLevelKong",ShowCatLevelKong)
}